package fatworm.planner;

import java.io.StringReader;

import fatworm.tx.Transaction;
import fatworm.parser.*;
import fatworm.query.*;

public class Planner {
   private QueryPlanner qplanner;
   private UpdatePlanner uplanner;
   
   public Planner(QueryPlanner qplanner, UpdatePlanner uplanner) {
	   this.qplanner = qplanner;
	   this.uplanner = uplanner;
   }
   
   public Plan createQueryPlan(String qry, Transaction tx) {
	   System.out.println(qry);
	   parser parser = new parser(new Lexer(new StringReader(qry)));
	   try {
		   parser.parse();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   return qplanner.createPlan(parser.Result, tx);
   }
   
   public int executeUpdate(String cmd, Transaction tx) {
	   parser parser = new parser(new Lexer(new StringReader(cmd)));
	   try {
		   parser.parse();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   return uplanner.execute(parser.Result, tx);
   }
}
